﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{
    partial class TabControl
    {

        /// <summary>
        /// 获取该选项卡控件中选项卡页的集合。
        /// </summary>
        [Category("行为")]
        [Description("获取该选项卡控件中选项卡页的集合。")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TabPageCollection TabPages { get; }

        /// <summary>
        /// 获取或设置控件显示的文字的字体。
        /// </summary>
        [DefaultValue(typeof(Font), "微软雅黑, 9pt")]
        public override Font Font
        {
            get => base.Font;
            set => base.Font = value;
        }

        /// <summary>
        /// 获取或设置页签标头显示之间的间隔。
        /// </summary>
        [DefaultValue(0)]
        [Category("外观"), Description("获取或设置页签标头显示之间的间隔。")]
        public int Interval
        {
            get => interval;
            set
            {
                if (interval != value)
                {
                    interval = value;
                    this.Invalidate();
                }
            }
        }

        /// <summary>
        /// 获取或设置首个页签显示的偏移。
        /// </summary>
        [DefaultValue(0)]
        [Category("外观"), Description("获取或设置首个页签显示的偏移。")]
        public int Offset
        {
            get => offset;
            set
            {
                if (offset != value)
                {
                    offset = value;
                    this.Invalidate();
                }
            }
        }

        /// <summary>
        /// 获取或设置选项卡在其中对齐的控件区域（例如，顶部边缘）。
        /// </summary>
        /// <returns>System.Windows.Forms.TabAlignment 值之一。 默认值为 Top。</returns>
        [DefaultValue(TabAlignment.Top)]
        [RefreshProperties(RefreshProperties.All)]
        [Category("行为")]
        [Description("获取或设置选项卡在其中对齐的控件区域（例如，顶部边缘）。")]
        public TabAlignment Alignment
        {
            get => alignment;
            set
            {
                alignment = value;
                refreshLayout();
            }
        }
        /// <summary>
        /// 获取或设置在控件的选项卡上显示的图像。
        /// </summary>
        /// <returns>System.Windows.Forms.ImageList，指定在选项卡上显示的图像。</returns>
        [DefaultValue(null)]
        public ImageList ImageList
        {
            get => m_ImageList;
            set
            {
                m_ImageList = value;
                foreach (TabPage tabPage in this.TabPages)
                    tabPage.ImageList = value;
            }
        }
        private ImageList m_ImageList;


        /// <summary>
        /// 获取该选项卡控件的标签处的间距。
        /// </summary>
        [Browsable(true)]
        [DefaultValue(typeof(Point), "6,3")]
        [Category("行为")]
        [Description("获取该选项卡控件的标签处的间距。")]
        [EditorBrowsable(EditorBrowsableState.Always)]
        public new Point Padding
        {
            get => m_padding;
            set
            {
                this.m_padding = value;
                this.refreshLayout();
            }
        }
        private Point m_padding = new Point(6, 3);

        /// <summary>
        /// 获取或设置控件的选项卡的大小。
        /// </summary>
        /// <returns>System.Drawing.Size，它表示选项卡的大小。 默认为自动调整选项卡的大小以适应选项卡上的图标和标签。</returns>
        public Size ItemSize
        {
            get
            {
                if (itemSize == default)
                {
                    switch (this.alignment)
                    {
                        case TabAlignment.Top:
                        case TabAlignment.Bottom:
                            return new Size(60, 30);
                        case TabAlignment.Left:
                        case TabAlignment.Right:
                            return new Size(30, 60);
                        default:
                            throw new NotImplementedException();
                    }
                }
                else
                    return itemSize;
            }
            set
            {
                itemSize = value;
                this.refreshLayout();
            }
        }

        private TabPage m_SelectedTab;

        /// <summary>
        /// 刷新布局
        /// </summary>
        private void refreshLayout()
        {
            int value;
            if (!this.ShowTabs)
            {
                base.Padding = new Padding(0, 0, 0, 0);
                return;
            }
            switch (this.alignment)
            {
                case TabAlignment.Top:
                    value = this.ItemSize.Height;
                    base.Padding = new Padding(0, value, 0, 0);
                    break;
                case TabAlignment.Bottom:
                    value = this.ItemSize.Height;
                    base.Padding = new Padding(0, 0, 0, value);
                    break;
                case TabAlignment.Left:
                    value = this.ItemSize.Width;
                    base.Padding = new Padding(value, 0, 0, 0);
                    break;
                case TabAlignment.Right:
                    value = this.ItemSize.Width;
                    base.Padding = new Padding(0, 0, value, 0);
                    break;
            }
            this.Refresh();
        }


        /// <summary>
        /// 获取或设置当前选定的选项卡页。
        /// </summary>
        [Browsable(true)]
        [DefaultValue(null)]
        [Category("行为")]
        [Description("获取或设置当前选定的选项卡页。")]
        public TabPage SelectedTab
        {
            get => m_SelectedTab;
            set
            {
                if (!Equals(this.m_SelectedTab, value))
                {
                    TabPage prevTabPage = this.m_SelectedTab;
                    TabPageChangingEventArgs tabPageChanging = new TabPageChangingEventArgs(this.m_SelectedTab, value);
                    SelectedTabChanging?.Invoke(this, tabPageChanging);

                    if (!tabPageChanging.Cancel)
                    {
                        if (value != null && this.TabPages.Contains(value))
                        {
                            value.Visible = true;
                            value.BringToFront();
                        }
                        if (this.m_SelectedTab != null)
                            this.m_SelectedTab.Visible = false;
                        this.m_SelectedTab = value;
                        TabPageChangedEventArgs tabPageChanged = new TabPageChangedEventArgs(this.m_SelectedTab, value);
                        SelectedTabChanged?.Invoke(this, tabPageChanging);
                        this.Invalidate();
                    }
                }
            }
        }

        private bool m_showTabs = true;
        /// <summary>
        ///     ''' 获取或设置是否显示选项卡的标题。
        ///     ''' </summary>
        ///     ''' <returns></returns>
        [DefaultValue(true)]
        [Category("行为")]
        [Description("获取或设置是否显示选项卡的标题。")]
        public bool ShowTabs
        {
            get => m_showTabs;
            set
            {
                m_showTabs = value;
                this.refreshLayout();
            }
        }

        /// <summary>
        /// 获取包含在控件内的控件的集合。
        /// </summary>
        /// <returns>System.Windows.Forms.Control.ControlCollection，它表示控件内包含的控件的集合。</returns>
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ControlCollection Controls => base.Controls;

        /// <summary>
        /// 获取或设置控件的背景色。
        /// </summary>
        [DefaultValue(typeof(Color), "White")]
        public new Color BackColor
        {
            get => this.m_BackColor;
            set
            {
                this.m_BackColor = value;
                base.BackColor = value;
            }
        }
        private Color m_BackColor = Color.White;
        private TabAlignment alignment = TabAlignment.Top;
        private Size itemSize;
        private int interval = 0;
        private int offset = 0;

        /// <summary>
        /// 获取或设置当鼠标指针位于控件上时显示的光标。
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Cursor Cursor
        {
            get => base.Cursor;
            set => base.Cursor = value;
        }
    }

}
